﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;


namespace Chianti.Service.Types {

    [DataContract]
    public class CreateDealResponse
    {
        [DataMember]
        public long DealID
        {
            get;
            set;
        }

        [DataMember]
        public List<Bid> CompetingBids
        {
            get;
            set;
        }
    }

    [DataContract]
    public class UpdateDealBidResponse
    {
        [DataMember]
        public List<Bid> CompetingBids
        {
            get;
            set;
        }
    }


    /// <summary>
    /// Public type exposed via the web service to contain information about competing bids
    /// </summary>
    [DataContract]
    public class Bid
    {
        [DataMember]
        public double BidAmount
        {
            get;
            set;
        }

        [DataMember]
        public DateTime ExpiryDate
        {
            get;
            set;
        }

        [DataMember]
        public double Distance
        {
            get;
            set;
        }

        
    }

    public partial class ActiveDeal
    {
        public static void InsertIntoDatabase(ChiantiDataClassesDataContext context, ActiveDeal activeDeal)
        {
            string query = string.Format("INSERT into ActiveDeals (DealID) VALUES ({0})", activeDeal.DealID);
            context.ExecuteCommand(query);
        }
    }


    public partial class Deal
    {
        /// <summary>
        /// Returns a boolean indicating whether the activeDeal passed in overlaps in area with this 
        /// deal instance
        /// </summary>
        /// <param name="activeDeal"></param>
        /// <returns></returns>
        internal bool DoesOverlap(Deal activeDeal)
        {
            Position thisLatitude = new Position(this.Business.Latitude);
            Position thisLongitude = new Position(this.Business.Longitude);

            Position activeDealLatitude = new Position(activeDeal.Business.Latitude);
            Position activeDealLongitude = new Position(activeDeal.Business.Longitude);

            double distanceBetweenDeals = PositionCalculator.GetDistanceInMeters(thisLongitude, thisLatitude, activeDealLongitude, activeDealLatitude);

            DealBid thisDealBid = null;
            DealBid activeDealBid = null;

            using (ChiantiDataClassesDataContext context = new ChiantiDataClassesDataContext())
            {
                thisDealBid = (from c in context.DealBids
                              where c.DealID == this.ID && c.IsActive == true
                              select c).First<DealBid>();

                activeDealBid = (from d in context.DealBids
                                 where d.DealID == activeDeal.ID && d.IsActive == true
                                 select d).First<DealBid>();


            }

            double boundaryDistance = thisDealBid.Radius + activeDealBid.Radius;

            if (distanceBetweenDeals < boundaryDistance)
            {
                return true;
            }
            else
            {
                return false;
            }

        }
    }
    [DataContract]
    public class Position
    {
        [DataMember]
        public int Degrees
        {
            get;
            set;
        }

        [DataMember]
        public double Minutes
        {
            get;
            set;
        }

        [DataMember]
        public double Seconds
        {
            get;
            set;
        }

        public double ToDecimal()
        {
            double retVal = 0;
            retVal = Math.Abs(Degrees) + (Minutes * 1 / 60) + (Seconds * 1 / 3600);
            if (Degrees < 0)
            {
                return retVal * -1;
            }

            return retVal;
        }



        public Position()
        {
        }

        public Position(double val)
        {
            double value = Math.Abs(val);

            this.Degrees = (int)value;

            double minutes = (value - this.Degrees) * 60;
            this.Minutes = (int)minutes;

            this.Seconds = (minutes - this.Minutes) * 60;

            if (val < 0)
            {
                Degrees = Degrees * -1;
            }
        }

    }
}

namespace Chianti.Service.Types.Enumeration
{
    [DataContract]
    public class DealEnumerationResult
    {
        [DataMember]
        public string Title
        {
            get;
            set;
        }

        [DataMember]
        public long ID
        {
            get;
            set;
        }

        [DataMember]
        public string Description
        {
            get;
            set;
        }

        [DataMember]
        public Business Business
        {
            get;
            set;
        }
        
    }
}